package datamanagers;


import commons.*;

import java.sql.*;

import config.ConfigManager;

public class DatasourceMySql {
	
	String host;
	int port;
	String schema;
	String user;
	String pass;
	
	public DatasourceMySql(String host,int port,String schema,String user,String pass){
		this.host = host;
		this.port = port;
		this.schema = schema;
		this.user = user;
		this.pass = pass;		
	}
	
	public DatasourceMySql() throws DataManagerException{
		try{
			ConfigManager config = ConfigManager.getInstance();
			this.host = config.get("db.host");
			this.port = Integer.parseInt(config.get("db.port"));
			this.schema = config.get("db.schema");
			this.user = config.get("db.username");
			this.pass = config.get("db.password");	
		} catch (Exception e) {
			throw new DataManagerException(e);
		}
		
	}
	
	public Connection getConnection() throws DataManagerException{

		Connection connection;
		String db_uri;
		db_uri = "jdbc:mysql://" 
				+ host + ":"
				+ port + "/" 
				+ schema +
				"?allowMultiQuery=true";
		//System.out.println(db_uri);
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(db_uri, user, pass);
			return connection;
		} catch (Exception e) {
			throw new DataManagerException(e);
		}

	}
}
